package whackEm.V2;



import whackEm.persistent.GameState;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

public class WhackEmActivity extends Activity {
    /** Called when the activity is first created. */

 private static final String TAG = WhackEmActivity.class.getSimpleName();
 private WhackEmPanel panel;
 private DisplayMetrics metrics;
 public static boolean FLAG_isPaused = false; 
 public boolean userExitedGame = false;
 PowerManager pm;
 WakeLock wl;
 GameState gs;
 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        metrics = getResources().getDisplayMetrics();
         
		gs = new GameState(this);	
        panel = new WhackEmPanel(this,metrics.widthPixels,metrics.heightPixels,metrics.density,gs);
        setContentView(R.layout.whackempanel);
        RelativeLayout ll1 =(RelativeLayout) findViewById(R.id.ll1);
        ll1.addView(panel);
        //setContentView(R.layout.main);
        Log.d(TAG, "onCreate");
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Keep it awake");
        wl.acquire();
        
        
    }
    


 @Override
 public void onStart(){
	 super.onStart();
	 Log.d(TAG,"onStart");
 }
 
 @Override
 public void onRestart(){
	 super.onRestart();
	 Log.d(TAG,"onRestart");	 
 }
 @Override
 public void onResume(){	
	 super.onResume();	
	 Log.d(TAG, "onResume");	 	
 }
 
 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
	 super.onSaveInstanceState(savedInstanceState);
	 Log.d(TAG, "onSaveInstance");      
 }

 @Override
 public void onRestoreInstanceState(Bundle savedInstanceState) {
	 super.onRestoreInstanceState(savedInstanceState);
	 Log.d(TAG, "onRestoreInstance");     
 }
 
 @Override
 public void onPause(){
	 super.onPause();
	 if (!userExitedGame)
		 FLAG_isPaused = true;	
	 panel.stopPLaying();
	 Log.d(TAG,"onpause");	 	
 }
 
    
 @Override
 public void onStop() {
  super.onStop();
  Log.d(TAG, "onStop..");
  gs.saveSharedpreferences();
  if (wl.isHeld())
	  wl.release();
 }
 
 
 @Override
 public void onDestroy() {
  super.onDestroy();
  Log.d(TAG, "onDestroy...");
 }
 


 @Override
 public boolean dispatchKeyEvent(KeyEvent event) {
     int action = event.getAction();
     int keyCode = event.getKeyCode();
         switch (keyCode) {
         case KeyEvent.KEYCODE_MENU:
        	 Log.d(TAG,"menu pressed");
         	 return true;         	         
         case KeyEvent.KEYCODE_BACK:
        	 Log.d(TAG,"back pressed");
        	 return true;
         case KeyEvent.KEYCODE_POWER:
        	 Log.d(TAG,"back pressed");
          	if (action == KeyEvent.ACTION_DOWN)
          	 panel.stopPLaying(); 
          	return true;
         default:
             return super.dispatchKeyEvent(event);
         }
   }
 
 
}
